<?php

namespace App\Http\Controllers\H5;

use App\Services\H5\TmpService;
use Illuminate\Http\Request;

class TmpController extends BaseController
{
    public function __construct(TmpService $tmp)
    {
        $this->db = $tmp;
    }

    /**
     * 获取一条
     */
    public function getOne(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $code = $request->post('code', '');
        $h5code = $request->post('h5code', '');
        $res = $this->db->getOneByCode($code, $h5code);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 评论列表获取
     */
    public function replyList(Request $request)
    {
        $code = $request->post('code', '');
        $rst = $this->db->replyList($code);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }

    /**
     * 评论新增
     */
    public function replyAdd(Request $request)
    {
        //数据验证
        $this->validate($request, [
            'cnt' => 'required|min:1|max:255',
            'h5code' => 'required',
            'code' => 'required',
        ], [
            'cnt.required' => '评论内容是必填的',
            'cnt.min' => '评论内容不能小于1个字符',
            'cnt.max' => '评论内容不能大于255个字符',
            'h5code' => '必须要登录',
            'code' => '码值必须的',
        ]);
       $headers = $this->uinfo($request);
        if ($headers['code'] != 0) {
            return $this->toJson(-2, $headers['msg']);
        }
        $h5code = $headers['data']['h5code'];
        $code = $headers['data']['code'];
        $cnt = $request->post('cnt', '');
        $res = $this->db->replyAdd($code, $h5code, $cnt);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 关注设置
     */
    public function followAdd(Request $request)
    {
        $headers = $this->uinfo($request);
        if ($headers['code'] != 0) {
            return $this->toJson(-2, $headers['msg']);
        }
        $code = $headers['data']['code'];
        $h5code = $headers['data']['h5code'];
        $res = $this->db->setFollow($code, $h5code);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //广告使用
    public function adUse(Request $request)
    {
        $headers = $this->uinfo($request);
        if ($headers['code'] != 0) {
            return $this->toJson(-2, $headers['msg']);
        }
        $use_id = $request->post('id', 0);
        $ad_id = $request->post('ad_id', 0);
        $h5code = $request->post('h5code', '');
        $code = $request->post('code', '');
        $this->db->setAdUse($ad_id, $use_id, $h5code, $code);
        return $this->toJson();
    }
}